package com.eb.controller;

import cn.hutool.core.bean.BeanUtil;
import com.eb.context.BaseContext;
import com.eb.domain.Innovation;
import com.eb.dto.AddInnovationDto;
import com.eb.dto.EditInnovationDto;
import com.eb.dto.InnovationListDto;
import com.eb.result.PageResult;
import com.eb.result.Result;
import com.eb.service.InnovationService;
import com.eb.vo.EditnnovationVo;
import com.eb.vo.InnovationDetailVo;
import com.eb.vo.InnovationVO;
import com.eb.vo.RankInnovationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/innovation")
@Api(tags = "帖子操作")
@CrossOrigin
@Slf4j
public class InnovationController {

    @Autowired
    private InnovationService innovationService;

    @PostMapping("/create")
    @ApiOperation("发送帖子")
    public Result create(@RequestBody AddInnovationDto addInnovationDto){
        boolean success = innovationService.createInnovation(addInnovationDto);
        if (success)
            return Result.success();
        return Result.error("发送失败");
    }

    @GetMapping("/selectDetails/{innovationId}")
    @ApiOperation("查看帖子详情")
    public Result selectDetails(@PathVariable Integer innovationId){
        InnovationDetailVo detailVo = innovationService.selectInnovationDetails(innovationId);
        return Result.success(detailVo);
    }
    @GetMapping("/list")
    @ApiOperation("查看帖子列表")
    public Result<PageResult> list(InnovationListDto innovationListDto){
       PageResult list = innovationService.selectInnovationVoList(innovationListDto);
       return Result.success(list);
    }
    @GetMapping("/seeInnovation/{innovationId}")
    @ApiOperation("编辑帖子的回显数据")
    public Result<EditnnovationVo> seeInnovation(@PathVariable Integer innovationId){
        Innovation innovation = innovationService.getById(innovationId);
        EditnnovationVo editInnovationVo = new EditnnovationVo();
        BeanUtil.copyProperties(innovation,editInnovationVo);
        return Result.success(editInnovationVo);
    }

    @PutMapping("/update")
    @ApiOperation("编辑帖子")
    public Result update(EditInnovationDto editInnovationDto){
        boolean success = innovationService.updateInnovation(editInnovationDto);
        if (success)
            return Result.success();
        return Result.error("编辑失败");
    }

    /**
     * 根据用户id获取用户发表的帖子列表
     * @return
     */
    @GetMapping("/userList")
    @ApiOperation("获取用户发表的帖子列表")
    public Result<List<InnovationVO>> userList(){
        log.info("获取id为{}的用户发表的帖子列表", BaseContext.getCurrentId());
        List<InnovationVO> List = innovationService.getInnovationByUserId(BaseContext.getCurrentId());
        return Result.success(List);
    }
    /**
     * 查询帖子热度排行
     */
    @GetMapping("/rank")
    @ApiOperation("获取排行榜")
    public Result<List<RankInnovationVO>> rank(){
      List<RankInnovationVO> rankInnovationVOS = innovationService.getRank();
      return Result.success(rankInnovationVOS);
    }


}
